package ar.com.photo_admin.domain

import grails.converters.*
import groovy.json.JsonSlurper

import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.DeleteMethod
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.PutMethod
import org.apache.commons.httpclient.methods.RequestEntity
import org.apache.commons.httpclient.methods.StringRequestEntity

import grails.plugins.springsecurity.Secured

import javax.annotation.PostConstruct

@Secured(["isAuthenticated()"])
class ShoppingCartController {

	def shoppingCartService
	def purchaseOrderService
	def springSecurityService
	
	User loggedInUser
	
	@PostConstruct
	void init() {
		loggedInUser = springSecurityService.getCurrentUser()
	}
	
	def show() {
		log.info "ShoppingCartController show"
		
		def photographer = null
		def photos = null
		
		def photographerId = params.int('photographerId')
		
		(photographer, photos) = photographerId == null ? shoppingCartService.getPhotographerOwnerOfMostPhotosInCartAndItsPhotosInJSON() : shoppingCartService.getPhotographerAndPhotographerPhotosInCartInJSON(photographerId)	
		
		// Gets the photographers that has at least one photo in the shopping cart of the logged in user. 
		def photographersWithPhotos = shoppingCartService.getPhotographersOwnerOfPhotosInCart()	
		
		def sizes = Size.list() as JSON
		def borders = PaperBorder.list() as JSON
		def paperTypes = PaperType.list() as JSON	
		
		[photos: photos, sizes: sizes, borders: borders, paperTypes: paperTypes, photographers: photographersWithPhotos, photographer: photographer]
	}
	
	def emptyShoppingCart(){		
		def photographerId = params.photographerCartToBeDeleted
		log.info "Clearing the shopping cart of the photographer: ${photographerId}"
		
		shoppingCartService.emptyShoppingCart(photographerId)
		redirect(controller:"index")	
	}
	
	def addPhoto() {
		log.info "Adding the photo ${params.photoI} to the shopping cart"
		shoppingCartService.addPhotoToCart(params.photoId)
		
		def hasPhotosInCart = shoppingCartService.hasPhotosInShoppingCart(loggedInUser)
		render (['photosStillInCart':hasPhotosInCart] as JSON)		
	}
	
	def removePhoto(){				
		log.info "Removing the photo ${params.photoId} from the shopping cart" 
		shoppingCartService.removePhotoFromShoppingCart(params.photoId)		
		
		def hasPhotosInCart = shoppingCartService.hasPhotosInShoppingCart(loggedInUser)
		render (['photosStillInCart':hasPhotosInCart] as JSON)		
	}
	
	def addAllPhotosFromAlbum() {
		log.info "Adding all the photos from the album ${params.albumId} to the shopping cart"
		shoppingCartService.addPhotosFromAlbum(params.albumId)
		
		def hasPhotosInCart = shoppingCartService.hasPhotosInShoppingCart(loggedInUser)
		render (['photosStillInCart':hasPhotosInCart] as JSON)		
	}
	
	def orderAndBuy() {
		flash.message = message(code: 'cart.register')
		render(success:true)
	}
	
	def order() {
		log.info "Saving purchase order"		
		def order = purchaseOrderService.saveJSONOrder(params.order)
	
		redirect(controller:"purchaseOrder", action:"confirmOrder", id:order.id)
	}
	
	def calculatePrice() {
		log.info "Updating the order price"		
		def  order = purchaseOrderService.parseFromJSON(params.order)
							
		render(order.price)
	}
}
